// import whoami from '@feidao/web/atom/browser/msg/whoami';
import get from '@dfeidao/atom-web/local/get';
import remove from '@dfeidao/atom-web/local/remove';
import fire from '@dfeidao/atom-web/msg/fire';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import error from '@dfeidao/atom-web/msgbox/error';
import success from '@dfeidao/atom-web/msgbox/success';
import s_get from '@dfeidao/atom-web/storage/get';
import { IFeidaoAiBrowserComponent } from '@dfeidao/web/interfaces';

export default async function a004(fd: IFeidaoAiBrowserComponent, args: { currentTarget: HTMLDivElement }) {
	//  保存
	const dom = args.currentTarget; // 获取DOM
	const stw = dom.getAttribute('data-feidao-switch'); // 获取开关的属性值
	if (stw === 'false') {   // 判断开关状态
		error('正在保存中');
		return;
	}
	dom.setAttribute('data-feidao-switch', 'false');// 在调用保存服务之前改变开关状态为false
	// const user = await whoami(get_user_info);

	const productid = await s_get('productid'); // 产品编号
	const _id = get(fd, 'id');
	const f = fd.data.node.querySelector<HTMLFormElement>('#pub_menu_form1');
	// const parent_no = get(fd, 'parent_no');  // 同级子级
	const system = fd.data.node.querySelector<HTMLSelectElement>('#zj3systemid');
	const systemid = system.value;
	const refsystem = fd.data.node.querySelector<HTMLSelectElement>('#zj3refsystemid');
	const refsystemid = refsystem.value;
	// const client_type = get(fd, 'client_point_id');
	// const client_type1 = fd.data.node.querySelector<HTMLSelectElement>('#zj3client_type');
	// const client_type = client_type1.value;
	const client_type = get(fd, 'client_point_id');

	const menu_name = f.menu_name.value;
	// const formname = f.formname.value;
	let priority = f.priority.value;
	const description = f.description.value;
	const icon = f.icon.value;
	const background = f.background.value;
	const n = fd.data.node.querySelector<HTMLInputElement>('[data-feidao-id="isshow"]');
	let is_show = true;
	if (n.checked === true) {
		is_show = true;
	} else {
		is_show = false;
	}
	// const package_no = get(fd, 'package_no');
	let formid = get(fd, 'package_no');
	if (!formid) {
		formid = get(fd, 'formid');
	}
	// console.log(client_type, '000', formid);

	if (!priority) {
		priority = 0;
	}
	if (!menu_name) {
		dom.setAttribute('data-feidao-switch', 'true');// 在拿到结果之后改变开关状态为true
		error('菜单名称不能为空');

		return;
	}
	if (!systemid) {
		dom.setAttribute('data-feidao-switch', 'true');// 在拿到结果之后改变开关状态为true
		error('systemid不能为空！');
		return;
	}
	// if (client_type === "请选择") {
	// 	dom.setAttribute('data-feidao-switch', 'true');// 在拿到结果之后改变开关状态为true
	// 	error('客户端类型不能为空');
	// 	return;
	// }
	if (!formid) {
		formid = '';
	}

	let res;
	if (menu_name === get(fd, 'menu_name')) {
		res = await nodejs('menu/zj-000003/s001', { systemid, refsystemid, client_type, priority, description, icon, productid, formid, is_show, background, _id });
	} else {
		res = await nodejs('menu/zj-000003/s001', { systemid, refsystemid, client_type, menu_name, priority, description, icon, productid, formid, is_show, background, _id });
	}

	if (res === 'success') {
		dom.setAttribute('data-feidao-switch', 'true');// 在拿到结果之后改变开关状态为true
		fd.data.node.classList.add('display-n');

		fire('zj-000001', 'eu-010');
		// fire('zj-000003', 'eu001');
		success('保存成功');
		remove(fd, 'package_no');
		remove(fd, 'package_name');
	} else if (res === 'fail') {
		dom.setAttribute('data-feidao-switch', 'true');// 在拿到结果之后改变开关状态为true
		error('保存失败');
		return;
	} else {
		dom.setAttribute('data-feidao-switch', 'true');
		error('菜单名称重复');
		return;
	}
}
